/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.datalogic.define.dao;

import cn.ekuma.data.dao.ModifiedLogDAO;
import com.openbravo.bean.AppUser;
import com.openbravo.bean.Role;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.PreparedSentence;
import com.openbravo.data.loader.SerializerWriteBasic;
import com.openbravo.data.loader.SerializerWriteString;
import com.openbravo.data.loader.StaticSentence;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class UserDAO extends ModifiedLogDAO<AppUser>{


    public UserDAO(I_Session s) {
        super(s);
    }

    @Override
    public TableDefinition getTable() {
        return  new TableDefinition(s,
            "PEOPLE",
             new Field[]{
                   new Field("ID",Datas.STRING,Formats.STRING),
                   new Field("NAME",Datas.STRING,Formats.STRING),
                   new Field("APPPASSWORD",Datas.STRING,Formats.STRING),
                   new Field("ROLE",Datas.STRING,Formats.STRING, Role.class),//AppLocal.getIntString("label.role")),
                   new Field("VISIBLE",Datas.BOOLEAN,Formats.BOOLEAN),
                   new Field("CARD",Datas.STRING,Formats.STRING),
                   new Field("NUMBERID",Datas.STRING,Formats.STRING),
                   new Field("LASTMODIFIED",Datas.TIMESTAMP,Formats.TIMESTAMP)
                 },
                 new int[] {0}
        );
    }

    @Override
    public void writeInsertValues(DataWrite dp, AppUser obj) throws BasicException {
          dp.setString(1,obj.getId());
          dp.setString(2,obj.getName());
          dp.setString(3, obj.getPassword());
          dp.setString(4, obj.getRole());
          dp.setBoolean(5,obj.isVisable());
          dp.setString(6,obj.getCard());
          dp.setString(7, obj.getNumber());
          dp.setTimestamp(8, obj.getLastModified());
    }

    public AppUser readValues(DataRead dr,AppUser obj) throws BasicException {
        if(obj==null)
        	obj=new AppUser();
        obj.setM_sId(dr.getString(1));
        obj.setM_sName(dr.getString(2));
        obj.setM_sPassword(dr.getString(3));
        obj.setM_sRole(dr.getString(4));
        obj.setIsVisable(dr.getBoolean(5));
        obj.setM_sCard(dr.getString(6));
        obj.setM_sNumber(dr.getString(7));
        obj.setLastModified(dr.getTimestamp(8));
         return obj;

    }

     public  final List listPeopleVisible() throws BasicException {
        return new StaticSentence(s
            , getTableDefinition().getListSQL()+" WHERE VISIBLE = " + s.getDB().TRUE()
            , null
            , this).list();
    }
    public final AppUser findPeopleByCard(String card) throws BasicException {
        return (AppUser) new PreparedSentence(s
            , getTableDefinition().getListSQL()+" WHERE CARD = ? AND VISIBLE = " + s.getDB().TRUE()
            , SerializerWriteString.INSTANCE
            , this).find(card);
    }

    public final AppUser findPeopleByName(String name) throws BasicException{
        return (AppUser)new PreparedSentence(s
            , getTableDefinition().getListSQL()+" WHERE NAME = ? AND VISIBLE = " + s.getDB().TRUE()
            , SerializerWriteString.INSTANCE
            , this).find(name);
    }

    public final AppUser findPeopleByNumberAndPass(String number,String pass) throws BasicException{
        return (AppUser)new PreparedSentence(s
            , getTableDefinition().getListSQL()+" WHERE NUMBERID = ? AND APPPASSWORD = ? AND VISIBLE = " + s.getDB().TRUE()
            , new SerializerWriteBasic(new Datas[] {Datas.STRING, Datas.STRING})
            , this).find(new Object[]{number,pass});
    }

    public final void execChangePassword(String id,String newPassword) throws BasicException {
        new StaticSentence(s
                , "UPDATE PEOPLE SET APPPASSWORD = ? WHERE ID = ?"
                ,new SerializerWriteBasic(new Datas[] {Datas.STRING, Datas.STRING})).exec(new String[]{newPassword,id});
    }

    @Override
    public Class getSuportClass() {
        return AppUser.class;
    }

	public AppUser findPeopleByIMSI(String imsi) throws BasicException {
		 return (AppUser)new PreparedSentence(s
		            , getTableDefinition().getListSQL()+" WHERE IMSI = ? AND VISIBLE = " + s.getDB().TRUE()
		            , SerializerWriteString.INSTANCE
		            , this).find(imsi);
	}

}
